/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package camada.apresentacao.dialog.cat52;

import camada.bancodedados.conexao.Conexao;
import camada.bancodedados.daoimplemente.cat52.ImportacaoCatDAOImpl;
import eloma.classe.cat52.ImportacaoCat;
import eloma.classe.cat52.RunnableImportacaoCat52;
import java.io.File;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import org.jdesktop.swingx.error.ErrorInfo;
import utilitarios.erros.MessagemErro;

/**
 *
 * @author Ícaro
 */
public class InternalFrmImportacaoCat52 extends javax.swing.JInternalFrame {

    private static Connection con;

    List<ImportacaoCat> listaImportacaoCat;

    int index = 0;
    boolean editar = false;
    boolean ePesquisa = false;
    int idImportacaoCat = 0;

    /**
     * Creates new form internalFrmCadCliente
     */
    public InternalFrmImportacaoCat52() {
        initComponents();
        capturaDados();
        carregarTabela();
        montarFields(index);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        jScrollPane1 = new javax.swing.JScrollPane();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        txtRegistro = new javax.swing.JTextField();
        jFileChooser = new javax.swing.JFileChooser();
        txtSeletor = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        btSelecionarPasta = new org.jdesktop.swingx.JXHyperlink();
        lbImportacao = new javax.swing.JLabel();
        progressBarAtividade1 = new javax.swing.JProgressBar();
        progressBarAtividade2 = new javax.swing.JProgressBar();
        jLabel3 = new javax.swing.JLabel();
        cmDataImportacao = new com.toedter.calendar.JDateChooser();
        jSeparator6 = new javax.swing.JSeparator();
        jScrollPane2 = new javax.swing.JScrollPane();
        tabelaCat52 = new javax.swing.JTable();
        jToolBar1 = new javax.swing.JToolBar();
        btNaoSeiAinda = new org.jdesktop.swingx.JXHyperlink();
        btNovo = new org.jdesktop.swingx.JXHyperlink();
        btSalvar = new org.jdesktop.swingx.JXHyperlink();
        btEditar = new org.jdesktop.swingx.JXHyperlink();
        btExcluir = new org.jdesktop.swingx.JXHyperlink();
        btCancelar = new org.jdesktop.swingx.JXHyperlink();
        jSeparator1 = new javax.swing.JToolBar.Separator();
        btPrimeiro = new org.jdesktop.swingx.JXHyperlink();
        btAnterior = new org.jdesktop.swingx.JXHyperlink();
        btProximo = new org.jdesktop.swingx.JXHyperlink();
        btUltimo = new org.jdesktop.swingx.JXHyperlink();
        jSeparator2 = new javax.swing.JToolBar.Separator();
        btPesquisa = new org.jdesktop.swingx.JXHyperlink();
        jXHyperlink12 = new org.jdesktop.swingx.JXHyperlink();
        jSeparator5 = new javax.swing.JToolBar.Separator();
        txtSair = new org.jdesktop.swingx.JXHyperlink();
        jSeparator3 = new javax.swing.JToolBar.Separator();
        btImportar = new org.jdesktop.swingx.JXHyperlink();
        jSeparator4 = new javax.swing.JSeparator();

        setMaximizable(true);
        setToolTipText("");

        jScrollPane1.setBorder(null);

        jPanel1.setToolTipText("Cadastro de Usuários");

        jLabel1.setText("Registro:");

        txtRegistro.setEnabled(false);

        txtSeletor.setEnabled(false);

        jLabel2.setText("Diretório arquivos Cat52:");

        btSelecionarPasta.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ima/outros/Seletor_Arquivos.png"))); // NOI18N
        btSelecionarPasta.setText("");
        btSelecionarPasta.setEnabled(false);
        btSelecionarPasta.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btSelecionarPastaActionPerformed(evt);
            }
        });

        lbImportacao.setText("Importação Cat52.");

        jLabel3.setText("Data Importação:");

        cmDataImportacao.setEnabled(false);

        tabelaCat52.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        jScrollPane2.setViewportView(tabelaCat52);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jSeparator6)
                    .addComponent(progressBarAtividade1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(progressBarAtividade2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(txtRegistro, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(94, 94, 94)
                                .addComponent(jFileChooser, javax.swing.GroupLayout.PREFERRED_SIZE, 1, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(jLabel1)
                            .addComponent(jLabel2)
                            .addComponent(lbImportacao)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(txtSeletor, javax.swing.GroupLayout.PREFERRED_SIZE, 375, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(btSelecionarPasta, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(jLabel3)
                            .addComponent(cmDataImportacao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 723, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(0, 611, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(7, 7, 7)
                        .addComponent(txtRegistro, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(17, 17, 17)
                        .addComponent(jFileChooser, javax.swing.GroupLayout.PREFERRED_SIZE, 2, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel3)
                .addGap(3, 3, 3)
                .addComponent(cmDataImportacao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtSeletor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btSelecionarPasta, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jSeparator6, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 335, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lbImportacao)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(progressBarAtividade1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(progressBarAtividade2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        jScrollPane1.setViewportView(jPanel1);

        jToolBar1.setFloatable(false);
        jToolBar1.setToolTipText("Cadastro Cliente");

        btNaoSeiAinda.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icones/internal/cad/document.png"))); // NOI18N
        btNaoSeiAinda.setText("");
        btNaoSeiAinda.setEnabled(false);
        btNaoSeiAinda.setFocusable(false);
        btNaoSeiAinda.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btNaoSeiAinda.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar1.add(btNaoSeiAinda);

        btNovo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icones/internal/cad/document_add.png"))); // NOI18N
        btNovo.setText("");
        btNovo.setFocusable(false);
        btNovo.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btNovo.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btNovo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btNovoActionPerformed(evt);
            }
        });
        jToolBar1.add(btNovo);

        btSalvar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icones/internal/cad/document_ok.png"))); // NOI18N
        btSalvar.setText("");
        btSalvar.setEnabled(false);
        btSalvar.setFocusable(false);
        btSalvar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btSalvar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btSalvar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btSalvarActionPerformed(evt);
            }
        });
        jToolBar1.add(btSalvar);

        btEditar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icones/internal/cad/document_edit.png"))); // NOI18N
        btEditar.setText("");
        btEditar.setFocusable(false);
        btEditar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btEditar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btEditar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btEditarActionPerformed(evt);
            }
        });
        jToolBar1.add(btEditar);

        btExcluir.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icones/internal/cad/document_error.png"))); // NOI18N
        btExcluir.setText("");
        btExcluir.setFocusable(false);
        btExcluir.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btExcluir.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btExcluir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btExcluirActionPerformed(evt);
            }
        });
        jToolBar1.add(btExcluir);

        btCancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icones/internal/cad/document_delete.png"))); // NOI18N
        btCancelar.setText("");
        btCancelar.setEnabled(false);
        btCancelar.setFocusable(false);
        btCancelar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btCancelar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btCancelarActionPerformed(evt);
            }
        });
        jToolBar1.add(btCancelar);
        jToolBar1.add(jSeparator1);

        btPrimeiro.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icones/internal/move/media_rewind.png"))); // NOI18N
        btPrimeiro.setText("");
        btPrimeiro.setFocusable(false);
        btPrimeiro.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btPrimeiro.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btPrimeiro.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btPrimeiroActionPerformed(evt);
            }
        });
        jToolBar1.add(btPrimeiro);

        btAnterior.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icones/internal/move/media_step_back.png"))); // NOI18N
        btAnterior.setText("");
        btAnterior.setFocusable(false);
        btAnterior.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btAnterior.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btAnterior.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btAnteriorActionPerformed(evt);
            }
        });
        jToolBar1.add(btAnterior);

        btProximo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icones/internal/move/media_step_forward.png"))); // NOI18N
        btProximo.setText("");
        btProximo.setFocusable(false);
        btProximo.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btProximo.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btProximo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btProximoActionPerformed(evt);
            }
        });
        jToolBar1.add(btProximo);

        btUltimo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icones/internal/move/media_fast_forward.png"))); // NOI18N
        btUltimo.setText("");
        btUltimo.setFocusable(false);
        btUltimo.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btUltimo.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btUltimo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btUltimoActionPerformed(evt);
            }
        });
        jToolBar1.add(btUltimo);
        jToolBar1.add(jSeparator2);

        btPesquisa.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icones/internal/print/document_view.png"))); // NOI18N
        btPesquisa.setText("");
        btPesquisa.setFocusable(false);
        btPesquisa.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btPesquisa.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btPesquisa.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btPesquisaActionPerformed(evt);
            }
        });
        jToolBar1.add(btPesquisa);

        jXHyperlink12.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icones/internal/print/printer.png"))); // NOI18N
        jXHyperlink12.setText("");
        jXHyperlink12.setFocusable(false);
        jXHyperlink12.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jXHyperlink12.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jXHyperlink12.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jXHyperlink12ActionPerformed(evt);
            }
        });
        jToolBar1.add(jXHyperlink12);
        jToolBar1.add(jSeparator5);

        txtSair.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icones/internal/move/window_delete.png"))); // NOI18N
        txtSair.setText("");
        txtSair.setFocusable(false);
        txtSair.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        txtSair.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        txtSair.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtSairActionPerformed(evt);
            }
        });
        jToolBar1.add(txtSair);
        jToolBar1.add(jSeparator3);

        btImportar.setText("importação");
        btImportar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btImportarActionPerformed(evt);
            }
        });
        jToolBar1.add(btImportar);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 1113, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jToolBar1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jSeparator4))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator4, javax.swing.GroupLayout.PREFERRED_SIZE, 5, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 570, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void txtSairActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtSairActionPerformed
        try {

            con = Conexao.getCon();
            con.rollback();

            this.setVisible(false);

        } catch (SQLException e) {
            MessagemErro erro = new MessagemErro(new ErrorInfo("Erro no Sistema", "Não é Possível Continuar, Sistema Inativo", null, "Fatal", e, Level.WARNING, null), 1);
        }
    }//GEN-LAST:event_txtSairActionPerformed

    private void btPrimeiroActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btPrimeiroActionPerformed
        if (ePesquisa == true) {
            capturaDados();
            index = 0;
            montarFields(index);
            ePesquisa = false;
        } else {
            index = 0;
            montarFields(index);
        }
    }//GEN-LAST:event_btPrimeiroActionPerformed

    private void btUltimoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btUltimoActionPerformed
        if (ePesquisa == true) {
            capturaDados();
            index = listaImportacaoCat.size() - 1;
            montarFields(index);
            ePesquisa = false;
        } else {
            index = listaImportacaoCat.size() - 1;
            montarFields(index);
        }
    }//GEN-LAST:event_btUltimoActionPerformed

    private void btProximoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btProximoActionPerformed
        if (ePesquisa == true) {
            capturaDados();
            if (index == (listaImportacaoCat.size() - 1)) {
                index = 0;
                montarFields(index);
            } else {
                index = index + 1;
                montarFields(index);
            }
            ePesquisa = false;
        } else {
            if (index == (listaImportacaoCat.size() - 1)) {
                index = 0;
                montarFields(index);
            } else {
                index = index + 1;
                montarFields(index);
            }
        }
    }//GEN-LAST:event_btProximoActionPerformed

    private void btAnteriorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btAnteriorActionPerformed
        if (ePesquisa == true) {
            capturaDados();
            if (index > 0) {
                index = index - 1;
                montarFields(index);
            } else if (index == 0) {
                index = listaImportacaoCat.size() - 1;
                montarFields(index);
            }
            ePesquisa = false;
        } else {
            if (index > 0) {
                index = index - 1;
                montarFields(index);
            } else if (index == 0) {
                index = listaImportacaoCat.size() - 1;
                montarFields(index);
            }
        }
    }//GEN-LAST:event_btAnteriorActionPerformed

    private void btPesquisaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btPesquisaActionPerformed
        try {

        } catch (Exception e) {
            MessagemErro erro = new MessagemErro(new ErrorInfo("Erro no Sistema", "Não é Possível Continuar, Sistema Inativo", null, "Fatal", e, Level.WARNING, null), 1);
        }
    }//GEN-LAST:event_btPesquisaActionPerformed

    private void btNovoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btNovoActionPerformed

        habilitaBotao();
        habilitarTextFiel();
        limparTextField();

        cmDataImportacao.setDate(new Date(System.currentTimeMillis()));

    }//GEN-LAST:event_btNovoActionPerformed

    private void btCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btCancelarActionPerformed

        desabilitaBotao();
        desabilitarTextFiel();

        montarFields(0);

    }//GEN-LAST:event_btCancelarActionPerformed

    private void btSalvarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btSalvarActionPerformed
        try {

            boolean validacao = validarTextField();

            if (validacao == false) {

                ImportacaoCatDAOImpl daoCat = new ImportacaoCatDAOImpl();

                ImportacaoCat d = new ImportacaoCat();

                d.setDataImportacao(new java.sql.Date(cmDataImportacao.getDate().getTime()));
                d.setDiretorio(txtSeletor.getText());

                if (editar == false) {
                   idImportacaoCat = daoCat.inserir(d);
                } else if (editar == true) {
                    d.setIdImportacaoCat52(Integer.parseInt(txtRegistro.getText()));
                    //daoBanco.atualizar(u);
                    editar = false;
                }

                desabilitaBotao();
                desabilitarTextFiel();

                listaImportacaoCat = daoCat.buscaTodos();

                index = listaImportacaoCat.size() - 1;
                montarFields(index);

            }

        } catch (Exception e) {
            MessagemErro erro = new MessagemErro(new ErrorInfo("Erro no Sistema", "Não é Possível Continuar, Sistema Inativo", null, "Fatal", e, Level.WARNING, null), 1);
        }
    }//GEN-LAST:event_btSalvarActionPerformed

    private void jXHyperlink12ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jXHyperlink12ActionPerformed
        try {
        } catch (Exception e) {
            MessagemErro erro = new MessagemErro(new ErrorInfo("Erro no Sistema", "Não é Possível Continuar, Sistema Inativo", null, "Fatal", e, Level.WARNING, null), 1);
        }
    }//GEN-LAST:event_jXHyperlink12ActionPerformed

    private void btEditarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btEditarActionPerformed
        try {

            if (Integer.parseInt(txtRegistro.getText()) > 0) {

                habilitaBotao();
                habilitarTextFiel();

                editar = true;
                idImportacaoCat = Integer.parseInt(txtRegistro.getText());
            }

        } catch (Exception e) {
            MessagemErro erro = new MessagemErro(new ErrorInfo("Erro no Sistema", "Não é Possível Continuar, Sistema Inativo", null, "Fatal", e, Level.WARNING, null), 1);
        }
    }//GEN-LAST:event_btEditarActionPerformed

    private void btExcluirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btExcluirActionPerformed
        try {

            ImportacaoCatDAOImpl daoCat = new ImportacaoCatDAOImpl();

            Object[] options = {"Confirmar", "Cancelar"};

            int i = JOptionPane.showOptionDialog(null,
                    "Excluir Banco?", "Excluir Banco",
                    JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null,
                    options, options[0]);

            if (i == JOptionPane.YES_OPTION) {

                try {

                    Object[] options2 = {"Confirmar", "Cancelar"};

                    int j = JOptionPane.showOptionDialog(null,
                            "Você Tem Certeza que Quer Excluir esse Banco?", "Excluir?",
                            JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null,
                            options2, options[0]);

                    if (j == JOptionPane.YES_OPTION) {

                        daoCat.remover(listaImportacaoCat.get(index));

                        capturaDados();
                        index = 0;
                        montarFields(index);
                    }

                } catch (Exception e) {
                    MessagemErro erro = new MessagemErro(new ErrorInfo("Erro no Sistema", "Não é Possível Continuar, Sistema Inativo", null, "Fatal", e, Level.WARNING, null), 1);
                }

            }

        } catch (Exception e) {
            MessagemErro erro = new MessagemErro(new ErrorInfo("Erro no Sistema", "Não é Possível Continuar, Sistema Inativo", null, "Fatal", e, Level.WARNING, null), 1);
        }
    }//GEN-LAST:event_btExcluirActionPerformed

    private void btSelecionarPastaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btSelecionarPastaActionPerformed
        try {

            jFileChooser.setCurrentDirectory(new File("."));
            jFileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
            int result = jFileChooser.showOpenDialog(this);

            if (result == JFileChooser.APPROVE_OPTION) {
                txtSeletor.setText(jFileChooser.getSelectedFile().getPath());
            }

        } catch (Exception e) {
            MessagemErro erro = new MessagemErro(new ErrorInfo("Erro no Sistema", "Não é Possível Continuar, Sistema Inativo", null, "Fatal", e, Level.WARNING, null), 1);
        }
    }//GEN-LAST:event_btSelecionarPastaActionPerformed

    private void btImportarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btImportarActionPerformed
        
        RunnableImportacaoCat52 runnableImportacao = new RunnableImportacaoCat52();

        try {

            List<String> listaVolta = new ArrayList<String>();

            if (!"".equals(txtSeletor.getText())) {

                lbImportacao.setText("Importação Cat52: Capturando Arquivos no Diretório Informado.");
                progressBarAtividade1.setMaximum(100);
                progressBarAtividade1.setString("Importação CAT52");
                progressBarAtividade1.setValue(5);
                progressBarAtividade2.setValue(100);

                File diretorio = new File(txtSeletor.getText());
                File[] file = diretorio.listFiles();

                for (int i = 0; i < file.length; i++) {
                    File arq = file[i];

                    progressBarAtividade2.setMaximum(i);
                    progressBarAtividade2.setValue(i);

                    if (arq.isDirectory()) {
                    } else {

                        int l = arq.getPath().length() - 3;

                        String inativo = arq.getPath().substring(l);

                        if (!"_IN".equals(inativo)) {
                            listaVolta.add(arq.getPath());
                        }
                    }
                }

            } else {
                MessagemErro erro = new MessagemErro(new ErrorInfo("Atenção", "Verifique o Campo do Diretório.", null, "Fatal", null, Level.INFO, null), 1);
            }

            if (listaVolta.size() > 0) {

                runnableImportacao.setStringArquivo(listaVolta);
                runnableImportacao.setProgressBar(progressBarAtividade1);
                runnableImportacao.setLabelResultados(lbImportacao);
                runnableImportacao.setProgressBar2(progressBarAtividade2);

                runnableImportacao.start();

            } else {

                lbImportacao.setText("Importação Cat52: O Diretório Pode Não Ter Arquivos ou os Arquivos já Foram Importados.");
                progressBarAtividade1.setValue(100);
                progressBarAtividade2.setValue(100);
            }

        } catch (Exception e) {
            MessagemErro erro = new MessagemErro(new ErrorInfo("Erro no Sistema", "Não é Possível Continuar, Sistema Inativo", null, "Fatal", e, Level.WARNING, null), 1);
            runnableImportacao.suspend();
        }
    }//GEN-LAST:event_btImportarActionPerformed

    private void capturaDados() {
        try {

            ImportacaoCatDAOImpl daoCat = new ImportacaoCatDAOImpl();

            listaImportacaoCat = daoCat.buscaTodos();

        } catch (Exception e) {
            MessagemErro erro = new MessagemErro(new ErrorInfo("Erro no Sistema", "Não é Possível Continuar, Sistema Inativo", null, "Fatal", e, Level.WARNING, null), 1);
        }
    }

    private void habilitaBotao() {

        btNaoSeiAinda.setEnabled(false);
        btNovo.setEnabled(false);
        btSalvar.setEnabled(true);
        btEditar.setEnabled(false);
        btExcluir.setEnabled(false);
        btCancelar.setEnabled(true);

        btPrimeiro.setEnabled(false);
        btProximo.setEnabled(false);
        btUltimo.setEnabled(false);
        btAnterior.setEnabled(false);

        btSelecionarPasta.setEnabled(true);

    }

    private void desabilitaBotao() {

        btNaoSeiAinda.setEnabled(true);
        btNovo.setEnabled(true);
        btSalvar.setEnabled(false);
        btEditar.setEnabled(true);
        btExcluir.setEnabled(true);
        btCancelar.setEnabled(false);

        btPrimeiro.setEnabled(true);
        btProximo.setEnabled(true);
        btUltimo.setEnabled(true);
        btAnterior.setEnabled(true);

        btSelecionarPasta.setEnabled(false);
    }

    private void habilitarTextFiel() {

        txtSeletor.setEnabled(true);

    }

    private void desabilitarTextFiel() {

        txtSeletor.setEnabled(false);

    }

    private void limparTextField() {

        txtRegistro.setText("");

    }

    private boolean validarTextField() {

//        if ("".equals(txtNome.getText())) {
//            txtNome.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(255, 0, 51), 2));
//            return true;
//        } else {
//            txtNome.setBorder(border);
//        }
//
//        if ("".equals(txtNumero.getText())) {
//            txtNumero.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(255, 0, 51), 2));
//            return true;
//        } else {
//            txtNumero.setBorder(border);
//        }
        return false;

    }

    private void montarFields(int i) {

        try {

            if (listaImportacaoCat.size() > 0) {

                ImportacaoCat b = listaImportacaoCat.get(i);

                txtRegistro.setText(String.valueOf(b.getIdImportacaoCat52()));
                cmDataImportacao.setDate(b.dataImportacao);
                txtSeletor.setText(b.getDiretorio());

            }

        } catch (Exception e) {
            MessagemErro erro = new MessagemErro(new ErrorInfo("Erro no Sistema", "Não é Possível Continuar, Sistema Inativo", null, "Fatal", e, Level.WARNING, null), 1);
        }

    }

    private void carregarTabela() {
//        try {
//
//            List<RegistroE01> listaRegistroE01 = daoCat52.selecionarTodasRegistro01();
//
//            tabelaCat52.setModel(new Cat52TableModel(listaRegistroE01));
//
//            tabelaCat52.setAutoCreateColumnsFromModel(false);
//            java.awt.FontMetrics fm = tabelaCat52.getFontMetrics(tabelaCat52.getFont());
//
//            tabelaCat52.setColumnModel(new Cat52ColumnModel(fm));
//            tabelaCat52.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
//
//        } catch (Exception e) {
//            MessagemErro erro = new MessagemErro(new ErrorInfo("Erro no Sistema", "Não é Possível Continuar, Sistema Inativo", null, "Fatal", e, Level.WARNING, null), 1);
//        }
    }


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private org.jdesktop.swingx.JXHyperlink btAnterior;
    private org.jdesktop.swingx.JXHyperlink btCancelar;
    private org.jdesktop.swingx.JXHyperlink btEditar;
    private org.jdesktop.swingx.JXHyperlink btExcluir;
    private org.jdesktop.swingx.JXHyperlink btImportar;
    private org.jdesktop.swingx.JXHyperlink btNaoSeiAinda;
    private org.jdesktop.swingx.JXHyperlink btNovo;
    private org.jdesktop.swingx.JXHyperlink btPesquisa;
    private org.jdesktop.swingx.JXHyperlink btPrimeiro;
    private org.jdesktop.swingx.JXHyperlink btProximo;
    private org.jdesktop.swingx.JXHyperlink btSalvar;
    private org.jdesktop.swingx.JXHyperlink btSelecionarPasta;
    private org.jdesktop.swingx.JXHyperlink btUltimo;
    private javax.swing.ButtonGroup buttonGroup1;
    private com.toedter.calendar.JDateChooser cmDataImportacao;
    private javax.swing.JFileChooser jFileChooser;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JToolBar.Separator jSeparator1;
    private javax.swing.JToolBar.Separator jSeparator2;
    private javax.swing.JToolBar.Separator jSeparator3;
    private javax.swing.JSeparator jSeparator4;
    private javax.swing.JToolBar.Separator jSeparator5;
    private javax.swing.JSeparator jSeparator6;
    private javax.swing.JToolBar jToolBar1;
    private org.jdesktop.swingx.JXHyperlink jXHyperlink12;
    private javax.swing.JLabel lbImportacao;
    private javax.swing.JProgressBar progressBarAtividade1;
    private javax.swing.JProgressBar progressBarAtividade2;
    private javax.swing.JTable tabelaCat52;
    private javax.swing.JTextField txtRegistro;
    private org.jdesktop.swingx.JXHyperlink txtSair;
    private javax.swing.JTextField txtSeletor;
    // End of variables declaration//GEN-END:variables

}
